package org.lq.ht.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.lq.ht.dao.RoomDao;
import org.lq.ht.entity.Room;
import org.lq.ht.util.Instantiation;
import org.lq.ht.util.JDBCUtil;
/**
 * 房间数据访问层接口实现
 *@author 张冲
 * @qq2865346830
 * @package org.lq.ht.dao.impl
 * @date 2020年8月21日下午8:04:01
 */
public class RoomDaoImpl  implements RoomDao,Instantiation<Room>{

	@Override
	public int save(Room t) {
		String sql ="insert into room (room_number,type,room_price,status,note,no_determined) values(?,?,?,?,?,?)";
		int num =0;
		try {
			num = JDBCUtil.executeUpdate(sql, t.getRoom_number(),t.getType(),t.getRoom_price(),t.getStatus(),t.getNote(),t.getNoDetermined());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int update(Room t) {
		String sql ="update room set room_number=?,type=?,room_price=?,status=?,note=?,no_determined = ? where no =?";
		int num =0;
		try {
			num =JDBCUtil.executeUpdate(sql, t.getRoom_number(),t.getType(),t.getRoom_price(),t.getStatus(),t.getNote(),t.getNoDetermined(),t.getNo());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int delete(int id) {
		String sql ="delete from room where no=?";
		int num =0;
		try {
			num=JDBCUtil.executeUpdate(sql, id);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return num;
	}

	@Override
	public List<Room> findAll() {
		List<Room> list = new ArrayList<Room>();
		String sql ="select * from room";
		try {
			list=JDBCUtil.executeQuery(sql, this);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Room getById(int id) {
		List<Room> list = new ArrayList<Room>();
		String sql ="select * from room where no =?";
		try {
			list=JDBCUtil.executeQuery(sql, this,id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(list.size()>0) {
			return list.get(0);
		}else {
			return null;
		}
	}

	@Override
	public Room instance(ResultSet rs) {
		Room r = new Room();
		try {
			r.setNo(rs.getInt("no"));
			r.setRoom_number(rs.getInt("room_number"));
			r.setType(rs.getInt("type"));
			r.setRoom_price(rs.getDouble("room_price"));
			r.setStatus(rs.getString("status"));
			r.setNote(rs.getString("note"));
			r.setNoDetermined(rs.getInt("no_determined"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return r;
	}

	@Override
	public List<Room> getByTypeID(int typeID) {
		try {
			return JDBCUtil.executeQuery("select *from room where type = ? and no_determined = 0", this,typeID );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Room> getByRoomNumber(int roomNumber) {
		List<Room> list = new ArrayList<>();
		try {
			return JDBCUtil.executeQuery("select *from room where room_number = ? and no_determined=0", this, roomNumber);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<Room> getByRoomPrice(double price) {
		try {
			return JDBCUtil.executeQuery("select *from room where room_price = ? and no_determined=0", this, price);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Room> getByRoomNote(String description) {
		try {
			return JDBCUtil.executeQuery("select *from room where note like ?and no_determined=0", this,"%"+description+"%");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	



}


